'use strict';

angular.module('fbdApp.templateModule')

.filter('templateFilter', function() {

  return function(ruleData, testData) {

    var transform = function(arr) {
      // var main = arr[0].name;
      var struct = {};
      struct = arr[0];
      struct['subset'] = {};
      var subStruct = struct['subset'];
      // subStruct = {};
      subStruct['rules'] = {};
      subStruct['includes'] = [];
      subStruct['before'] = [];
      subStruct['after'] = [];
      subStruct['require'] = [];

      for (var i = 1; i < arr.length; i++) {
        // var res = {};
        var curr = arr[i];
        if (curr.length == undefined) { //非子模板

          subStruct['rules'][curr.name] = curr;

          subStruct['includes'].push(curr.name);

          subStruct['require'].push(curr.name);
          // struct.includes =
        } else { //子模板
          subStruct['rules'][curr[0].name] = transform(curr);
          subStruct['includes'].push(curr[0].name);

          // subStruct['require'].push(curr[0].name);
          subStruct['after'].push(curr[0].name);
        }
      }

      return struct;
    }

    var postData = {};
    var main = ruleData[0].name;
    postData.text = testData;
    postData.template = {
      rules: {},
      includes: [main],
      before: [],
      after: [],
      require: [main]
    };

    postData['template']['rules'][main] = transform(ruleData);

    return postData;
  };
});
